package com.example.common.config;

import com.example.common.properties.RedisProperties;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration("commRedisConfig")
@Slf4j
public class RedisConfig {

    @Autowired
    private RedisProperties redisProperties;

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        log.info("正在创建Redis连接工厂...");
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName(redisProperties.getHost());
        config.setPort(redisProperties.getPort());
        config.setPassword(redisProperties.getPassword());
        log.info("Redis连接配置: host={}, port={}, password={}",
                config.getHostName(), config.getPort(), config.getPassword());

        LettuceConnectionFactory factory = new LettuceConnectionFactory(config);
        factory.afterPropertiesSet();
        log.info("Redis连接工厂创建完成");
        return factory;
    }

    @Bean(destroyMethod = "shutdown")
    @RefreshScope
    public RedissonClient redisson() throws Exception {
        log.info("正在创建Redisson客户端...");
        log.info("Redis配置信息: host={}, port={}, password={}",
                redisProperties.getHost(), redisProperties.getPort(), redisProperties.getPassword());

        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort())
                .setPassword(redisProperties.getPassword())
                .setConnectTimeout(5000)
                .setRetryAttempts(3)
                .setRetryInterval(1500);

        RedissonClient client = Redisson.create(config);
        log.info("Redisson客户端创建完成");
        return client;
    }

    // 配置 Redisson 客户端         //redis的高级运用，锁
//    @Bean("commRedissonClient")
//    public RedissonClient redissonClient() {
//        // 配置
//        Config config = new Config();
////        config.useSingleServer().setAddress("redis://127.0.0.1:6379").setPassword("607607");
//        config.useSingleServer()
//                .setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort())
//                .setPassword(redisProperties.getPassword());
//        // 创建RedissonClient对象
//        return Redisson.create(config);
//    }

    @Bean("commStringRedisTemplate")
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory) {
        log.info("正在创建StringRedisTemplate...");
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(connectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        log.info("StringRedisTemplate创建完成");
        return template;
    }

    @Bean("commRedisTemplate")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        log.info("正在创建RedisTemplate...");
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        log.info("RedisTemplate创建完成");
        return template;
    }
}
